<?php

namespace App\Http\Controllers;

use App\Common\Exception\ErrorException;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Faculty\PlatformCredit;
use App\Models\ActivityInfo;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }


    /**
     * 主页
     */
    public function index()
    {
        $user = Auth::user();
        //教育教学
        $ActivityInfo3 = ActivityInfo::select('activity_info.*', 'b.status')
            ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')->where('activity_info.deleted', 0)
            ->leftJoin('participant as p', 'activity_info.id', '=', 'p.activity_info_id')
            ->manageable()->distinct('activity_info.id');
        $jsonSearch['pubications_publish_type'] = 128;//文章发表
        $jsonSearch2['pubications_publish_type'] = 129;//论著发表
        $result = [
            'job' => [
                'djqk' => $this->getActivityInfo()->where('activity_info.activity_category','IMPORTANTWORK_TEACHING_SITUATION_TABLE')->count(),//带教情况
                'xngz' => $this->getActivityInfo()->where('activity_info.activity_category','IMPORTANTWORK_IN_SCHOOL_TABLE')->count(),//校内工作
                'xwgz' => $this->getActivityInfo()->where('activity_info.activity_category','IMPORTANTWORK_OUT_SCHOOL_TABLE')->count(),//校外工作
            ],
            'train' => [
                'xbpx' => PlatformCredit::where('user_id', '>', 0)->when(!$user->is_checker, function($q) use($user){
                    $q->where('user_id', $user->teacher?$user->teacher->id:-1);
                })->count(),//校本培训
                'cyky' => $this->getActivityInfo()->where('activity_info.activity_category','TEACHERTRAINING_JOINTRAINING_TABLE')->count(),//参与考研
                'wcpx' => $this->getActivityInfo()->where('activity_info.activity_category','TEACHERTRAINING_OUTTRAINING_TABLE')->count(),//外出培训
                'qtpx' => $this->getActivityInfo()->where('activity_info.activity_category','TEACHERTRAINING_OTHER_TABLE')->count(),//其他培训
            ],
            'award' => [
                'tdhj' => $this->getAwardActivityInfo(236)->count(),
                'rych' => $this->getAwardActivityInfo(145)->count(),
                'ggch' => $this->getAwardActivityInfo(146)->count(),
                'glpb' => $this->getAwardActivityInfo(147)->count(),
                'bjhj' => $this->getAwardActivityInfo(148)->count(),
                'qthj' => $this->getAwardActivityInfo(849)->count(),
                'glzdj' => $this->getAwardActivityInfo(150)->count(),
            ],
            'teach' => [
                'gkjx' => $this->getActivityInfo()->whereIn('activity_info.activity_category',['TEARRESEARCH_OPEN_TABLE','EW_CLASS_MEETING_TABLE'])->count(),//公开教学
                'jyjl' => $this->getActivityInfo()->whereIn('activity_info.activity_category',['EW_EXCHANGE_EXPERIENCE_TABLE','TEARRESEARCH_EXCHANGE_TABLE'])->count(),//经验交流
                'ktyj' => $ActivityInfo3->where('activity_info.activity_category','EW_SUBJECT_RESEARCH_TABLE')->count(),//课题研究
                'lwfb' => $this->getActivityInfo()->whereIn('activity_info.activity_category',['EW_PAPER_PUBLISH_TABLE','TEARRESEARCH_PUBLISH_TABLE'])->when(!empty($jsonSearch), function($q) use($jsonSearch) {
                    foreach($jsonSearch as $k => $id) {
                        $q->whereRaw(" JSON_EXTRACT(activity_info.`".$k."`,'$[0].optionId') = $id");
                    }
                })->count(),//文章发表
                'lzfb' => $this->getActivityInfo()->whereIn('activity_info.activity_category',['EW_PAPER_PUBLISH_TABLE','TEARRESEARCH_PUBLISH_TABLE'])->when(!empty($jsonSearch2), function($q) use($jsonSearch2) {
                    foreach($jsonSearch2 as $k => $id) {
                        $q->whereRaw(" JSON_EXTRACT(activity_info.`".$k."`,'$[0].optionId') = $id");
                    }
                })->count(),//论著发表
                'kfkm' => $this->getActivityInfo()->where('activity_info.activity_category','TEARRESEARCH_DEVELOP_SUBJECT_TABLE')->count(),//开发科目
            ],
        ];
        return view('home.index', $result)->with('data', $result);
    }

    function getActivityInfo()
    {
        return ActivityInfo::select('activity_info.*', 'b.status')
            ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')->where('b.status','PASS')->where('activity_info.deleted', 0)
            ->leftJoin('participant as p', 'activity_info.id', '=', 'p.activity_info_id')
            ->manageable()->distinct('activity_info.id');

    }

    function getAwardActivityInfo($optionId)
    {
        return ActivityInfo::select('activity_info.*', 'b.status')
            ->leftJoin('form_instance as b' ,'activity_info.form_instance_id','=','b.id')->where('b.status','PASS')->where('activity_info.deleted', 0)
            ->leftJoin('participant as p', 'activity_info.id', '=', 'p.activity_info_id')
            ->manageable()->distinct('activity_info.id')
            ->where('activity_info.activity_category','TEACHER_AWARD_TABLE')
                ->whereRaw(" JSON_EXTRACT(activity_info.`category`,'$[0].optionId') = {$optionId}");

    }
}
